import { getUserProfile, request } from '../../utils/http'

Page({
  data: {
    user: {},
    recentResults: {},
    latestQuizList: []
  },

  getUserProfile () {
    getUserProfile().then(data => {
      this.selectComponent('#chart').update(data.ratings.masterNode)
      this.setData({
        user: data.user
      })
    })
  },

  getRecentResults () {
    request('/api/portal/homePage/achievementAnswers/recentStatistics').then(data => {
      this.setData({
        recentResults: {
          ...data,
          accuracy: data.total ? ((data.correct || 0) / data.total * 100).toFixed(2) : 0
        }
      })
    })
  },

  getLatestQuizList () {
    request('/api/portal/exam/exams/newest').then(data => {
      this.setData({
        latestQuizList: data.content
      })
    })
  },

  goHistory () {
    wx.switchTab({
      url: '../history/index'
    })
  },

  goSettings () {
    wx.switchTab({
      url: '../settings/index'
    })
  },

  onShow: function (options) {
    this.getUserProfile()
    this.getRecentResults()
    this.getLatestQuizList()
  }
})
